//
//  BoundPhoneViewController.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/3/24.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "BoundPhoneViewController.h"

@interface BoundPhoneViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *verifyField;
@property (weak, nonatomic) IBOutlet UIButton *getVerifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;


@end

@implementation BoundPhoneViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.navigationItem.title = @"绑定手机号";
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取验证码
- (IBAction)Verify:(id)sender {
    
    if (_phoneField.text.length == 0) {
        [Utils alertViewWithMessage:@"填写手机号"];
        return;
    }
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_VerifyCode_get" forKey:@"method"];
    [dict setObject:@"4" forKey:@"type"];
    [dict setObject:_phoneField.text forKey:@"mobile"];
    [Utils get:nil parameters:dict success:^(NSInteger recode ,id jsonObject){
        NSLog(@"%@", jsonObject);
        //60s倒计时
        /*启动计时器*/
        self->ti= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
        self->time=60;
        
        self->_getVerifyBtn.enabled=NO;
        [self->_getVerifyBtn setBackgroundColor:[Utils stringToColor:@"dddddd"]];
        [self->_getVerifyBtn setTitleColor:[Utils stringToColor:@"979797"] forState:UIControlStateNormal];
        [self->_getVerifyBtn setTitle:[NSString stringWithFormat:@"等待%d秒",self->time] forState:UIControlStateNormal];
    } failure:^(NSInteger recode , NSError * error){
        
    }];

}
/**
 *  编辑人：张东海
 *  时间：2016年5月26日
 *  更改或添加内容：计时器调用方法
 *  备注：
 */

-(void)timerFireMethod:(NSTimer *)timer{
    
    time-=1;
    [self->_getVerifyBtn setTitle:[NSString stringWithFormat:@"等待%d秒",time] forState:UIControlStateNormal];
    // lab.text=[NSString stringWithFormat:@"等待%d秒",time];
    if (time==0) {
        self->_getVerifyBtn.enabled=YES;
        [self->_getVerifyBtn setBackgroundColor:[Utils stringToColor:@"fffbb5"]];
        [self->_getVerifyBtn setTitleColor:[Utils stringToColor:@"ae7d3d "] forState:UIControlStateNormal];
        

        [self->_getVerifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        if ([timer isValid] == YES) {
            [timer invalidate];
            timer = nil;
        }
        
        
    }
    
}
- (IBAction)confirm:(id)sender {
    
    if (_phoneField.text.length == 0 || _verifyField.text.length == 0) {
        
        [Utils alertViewWithMessage:@"请将信息填写完整"];
        return;
    }
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_usermobile_edit" forKey:@"method"];
    [dict setObject:_verifyField.text forKey:@"verify"];
    [dict setObject:_phoneField.text forKey:@"mobile"];


    [Utils get:nil parameters:dict success:^(NSInteger recode,id jsonObject){
        
        
        NSDictionary * dict = (NSDictionary *)jsonObject;
        [Utils alertViewWithMessage:[dict objectForKey:@"re_info"]];
        NSDictionary * personalImformation = [dict objectForKey:@"re_data"];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"绑定手机号成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        
    } failure:^(NSInteger recode, NSError * error){
    }];

    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
